# 转换项目文件为 UTF-8 编码（无 BOM）脚本

$files = Get-ChildItem -Recurse -File -Include *.ts,*.js,*.json,*.md,*.sql,*.txt,.env* `
    | Where-Object { 
        $_.FullName -notlike "*node_modules*" -and 
        $_.FullName -notlike "*dist*" -and
        $_.FullName -notlike "*\.git*"
    }

$convertedCount = 0
$errorCount = 0

Write-Host "开始转换文件为 UTF-8（无 BOM）..." -ForegroundColor Cyan
Write-Host ""

foreach ($file in $files) {
    try {
        # 读取文件内容
        $content = Get-Content $file.FullName -Raw -Encoding UTF8
        
        # 使用 UTF-8 无 BOM 编码保存
        $utf8NoBom = New-Object System.Text.UTF8Encoding $false
        [System.IO.File]::WriteAllText($file.FullName, $content, $utf8NoBom)
        
        $convertedCount++
        Write-Host "✅ $($file.Name)" -ForegroundColor Green
    } catch {
        $errorCount++
        Write-Host "❌ $($file.Name): $_" -ForegroundColor Red
    }
}

Write-Host ""
Write-Host "转换完成:" -ForegroundColor Cyan
Write-Host "  ✅ 成功: $convertedCount" -ForegroundColor Green
Write-Host "  ❌ 失败: $errorCount" -ForegroundColor Red

if ($errorCount -eq 0) {
    Write-Host ""
    Write-Host "✅ 所有文件已转换为 UTF-8（无 BOM）" -ForegroundColor Green
    exit 0
} else {
    exit 1
}

